<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        ul {
            list-style: none
        }

        img {
            vertical-align: top
        }

        .box {
            width: 490px;
            height: 170px;
            margin: 100px auto;
            padding: 5px;
            border: 1px solid #ccc;
        }

        .inner {
            width: 490px;
            height: 170px;
            background-color: pink;
            overflow: hidden;
            position: relative;
        }

        .inner ul {
            width: 1000%;
            position: absolute;
            top: 0;
            left: 0;
        }

        .inner li {
            float: left;
        }

        .square {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }

        .square span {
            display: inline-block;
            width: 16px;
            height: 16px;
            background-color: #fff;
            text-align: center;
            line-height: 16px;
            cursor: pointer;
        }

        .square span.current {
            background-color: red;
            color: #fff;
        }




    </style>

</head>
<body>
<div class="box" id="box">
    <div class="inner">
        <ul>
            <li><a href="#"><img src="images/01.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/02.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/03.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/04.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/05.jpg" alt=""/></a></li>
        </ul>
        <div class="square">
            <span class="current">1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
        </div>
    </div>
</div>

<div id="test"></div>

<script>
    const box=document.getElementById('box')
    const ulBox=document.querySelector('ul')
    const square=document.querySelector('.square')

    //获取当前li的宽度
    const liBoxLength=document.querySelector('li').offsetWidth


    square.addEventListener('click',(el)=>{
        //当前元素
        const currentEl=el.target

        //获取索引
        const currentIndex=+currentEl.innerText-1

        //点击按钮 激活
        Array.from(square.children).forEach((item)=>{
            item.classList.remove('current')
        })
        currentEl.classList.add('current')

        // 位移的长度
        const moveLength=currentIndex*liBoxLength

        console.log(moveLength)

        moveAnimation(ulBox,-moveLength)

    })


    //实现移动动画
    function moveAnimation(el,target){
        clearInterval(el.timer)
       let currentPosition=el.offsetLeft;
        const step=currentPosition<target?3:-3
        console.log(currentPosition)
        console.log(step)
        el.timer=setInterval(()=>{
                if(Math.abs(target-currentPosition)>Math.abs(step)){
                    currentPosition+=step
                    el.style.left=`${currentPosition}px`
                    console.log(currentPosition)
                }else{
                    el.style.left=`${target}px`
                    clearInterval(el.timer)
                }

        },10)
    }


</script>
</body>
</html>
